<template>
    <div class="page-main">
        <div class="header-container">
            <router-link to="/MyInfo" class="back-button">
                <img src="@/assets/images/mine/back.png" alt="返回图标" class="return-image" />
            </router-link>
            <p>推文点赞</p>
        </div>
        <div class="waterfall-container">
            <div class="waterfall-column" v-for="(column, index) in waterfallColumns" :key="index">
                <div v-for="card in column" :key="card.postId" class="card-item" @click="goToDetail(card.postId)">
                    <div class="card-img">
                        <img :src="card.firstImageUrl" alt="文章图片" />
                    </div>
                    <div class="card-content">
                      <div class="title-row">
                            <img class="avatar" :src="card.creatorPic" alt="用户头像" />
                            <h3 class="title">{{ card.title }}</h3>
                        </div>
                        <div class="content" >{{ stripHtml(card.content) }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import{getpostInfo}from '../api/index';
const router = useRouter();
const cardList = ref([]);
const waterfallColumns = ref([[], []]); // 默认两列
const getTweetList1 = () => {
  getpostInfo().then((res) => {
        cardList.value = res.data.data;
        
    }).catch((error) => {
        showToast('获取文章列表失败，请稍后重试' + error);
    });
};
watch(cardList, () => {
    distributeCards();
});
const goToDetail = (id) => {
    router.push({
        name: 'ProtDetail',
        params: { id }
    });
};
const stripHtml = (html) => {
    if (!html) return '';

    // 第一步：解析 HTML 实体编码
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, 'text/html');
    const parsedText = doc.body.textContent || '';

    // 第二步：删除所有空白字符并修剪前后空格
    return parsedText.replace(/\s+/g, ' ').trim();
};

// 分配卡片到列中，实现瀑布流效果
const distributeCards = () => {
    const columns = [[], []]; // 两列
    cardList.value.forEach((card, index) => {
        // 根据索引奇偶性分配到不同列
        const columnIndex = index % 2;
        columns[columnIndex].push(card);
    });
    waterfallColumns.value = columns;
};
onMounted(() => {
  getTweetList1();
});
</script>
<style lang="scss" scoped>
.page-main {
    background: #ffffff;
    min-height: 100vh;
    padding: 60px 10px 10px; /* 新增上边距 */

    .header-container {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 60px;
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        z-index: 1000;
    }

    .back-button {
        position: absolute;
        top: 10px;
        left: 10px;
        display: flex;
        align-items: center;
        text-decoration: none;
        color: #333;
    }

    .header-container p {
        font-size: 20px;
        font-weight: bold;
        color: #333;
        margin: 0;
        text-align: center;
    }

    .waterfall-container {
        margin-top: 10px; /* 优化瀑布流间距 */
        display: flex;
        justify-content: space-between;
        padding: 0;

        .waterfall-column {
            flex: 1;
            max-width: calc(50% - 5px);

            &:first-child {
                margin-right: 5px;
            }

            &:last-child {
                margin-left: 5px;
            }
        }

        .card-item {
            text-align: center;
            border-radius: 8px;
            margin-bottom: 10px;
            background: #f3f0f0;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
            display: flex;
            flex-direction: column;
            overflow: hidden;
            height: 320px; // 设置固定高度


            .card-img {
                height: 250px;
                flex-shrink: 0;

                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                    border-radius: 8px;
                }
            }

            .card-content {
                padding: 2px;
                height: 70px; // 设置内容区域固定高度
                display: flex;
                flex-direction: column;

                .title-row {
                    display: flex;
                    align-items: center;
                    gap: 8px;
                    margin: 2px;
                    height: 34px; // 设置标题行高度

                    .avatar {
                        width: 30px;
                        height: 30px;
                        border-radius: 50%;
                        flex-shrink: 0;
                    }

                    .title {
                        font-size: 13px;
                        font-weight: bold;
                        color: #333;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        text-align: left;
                        flex: 1;
                    }
                }

                .content {
                    font-size: 10px;
                    color: #666;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    margin: 2px;
                    text-align: left;
                    padding-right: 8px;
                    height: 36px; // 设置内容区域高度，确保两行文字的显示空间
                }

                .time {
                    font-size: 12px;
                    color: #999;
                    align-self: flex-end;
                    margin: 2px;
                }
            }
        }
    }
}



</style>